<?php

/**
 * Tag cloud widget class
 *
 * @since 2.8.0
 */
class nivo_slider extends WP_Widget {

    function nivo_slider() {
        $this->WP_Widget(false, $name = 'EB Nivo Slider ');
    }

    function widget($args, $instance) {
        ?>

        <div class="main p12b p5t">
            <div class="slide gray_border po theme-default">
                <div id="slider" class="nivoSlider">
                    <?php
                    wp_reset_query();
                    query_posts(array('cat' => $instance['slider_cat']));
                    while (have_posts()): the_post();
                        $default_attr = array(
                            'alt' => get_the_title(),
                            'title' => get_the_excerpt(),
                        );
                        echo the_post_thumbnail(array($instance['width'], $instance['height']), $default_attr);
                    endwhile;
                    wp_reset_query();
                    ?>

                </div> 

            </div>
        </div>

        <script type="text/javascript">
            jQuery(document).ready(function(){
                $('#slider').nivoSlider({pauseTime: 10000});
            });
        </script>

        <?php
    }

    function update($new_instance, $old_instance) {

        $search = array("\\'", '\\"');
        $replace = array("'", '"');
        $data = str_replace($search, $replace, $_POST);

        return $data;
    }

    function form($instance) {
        ?>
        <p>
            <label for=""><?php _e('Slider category:'); ?></label>
            <select name="slider_cat"  class="widefat">
                <?php
                $category_ids = get_all_category_ids();
                foreach ($category_ids as $row) {
                    $cat_name = get_cat_name($row);
                    ?>
                    <option value="<?php echo $row ?>" <?php echo $instance['slider_cat'] == $row ? 'selected' : '' ?>><?php echo $cat_name; ?></option>
        <?php } ?>
            </select>

        </p>
        <p>
            <label for="">Width</label> <br>
            <input type="text" name="width" class="widefat"  value="<?php echo $instance['width'] ?>">
        </p>
        <p>
            <label for="">Height</label><br>
            <input type="text" name="height" class="widefat" value="<?php echo $instance['height'] ?>">
        </p>


        <?php
    }

}
